import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import eu from '@/components/ts/enum.js'
import systemdemo from '@/components/ts/systemdemo.js'


// 所有可选的修正参数
export const allCorrectParas = [
  {
    name: '大气压力',
    x: ['大气压力值_kPa', '大气压力变化量_kPa'],
    y: ['乘法修正系数', '除法修正系数'],
    unit: ['kPa', 'bar'],
    '描述': '这是大气压力的描述'
  },
  {
    name: '大气温度',
    x: ['大气温度值', '大气温度变化量'],
    y: ['乘法修正系数', '除法修正系数'],
    unit: ['℃', 'K'],
    '描述': '这是大气温度的描述'
  },
  {
    name: '环境相对湿度',
    x: ['环境湿度值', '环境湿度变化量'],
    y: ['乘法修正系数', '除法修正系数']
  },
  {
    name: '燃料气低位热值',
    x: ['燃料气热值', '燃料气热值变化量'],
    y: ['乘法修正系数', '除法修正系数']
  },
  {
    name: '背压'
  },
  {
    name: '循环水温度对功率',
    x: ['循环水温度值', '循环水温度变化量'],
    y: ['乘法修正系数', '除法修正系数', '加法修正量']
  },
  {
    name: '循环水流量对功率',
    x: ['循环水流量值', '循环水流量变化量', '循环水流量偏差百分比'],
    y: ['乘法修正系数', '除法修正系数', '加法修正量']
  },
  {
    name: '燃料气温度',
    x: ['燃料气温度值', '燃料气温度变化量'],
    y: ['乘法修正系数', '除法修正系数']
  },
  {
    name: '天然气压力'
  },

  {
    name: '燃机发电机氢压'
  },
  {
    name: '发电机转速',
    x: ['发电机转速值_rpm', '发电机转速变化量_rpm'],
    y: ['乘法修正系数', '除法修正系数'],
    z: ['环境温度_℃', '空值']
  },
  {
    name: '燃机发电机频率'
  },
  {
    name: '燃机发电机功率因数'
  },
  {
    name: '汽机发电机氢压'
  },
  {
    name: '汽机发电机频率'
  },
  {
    name: '汽机发电机功率因数'
  },
  {
    name: '老化修正'
  },
  {
    name: '补水率'
  },
  {
    name: '系统泄漏量对功率',
    y: ['乘法修正系数', '除法修正系数', '加法修正量']
  }
] as const

// 所有可选的修正参数
export const allLeakageParas = [
  {
    name: '高压门杆',
    一漏至: ['高排'],
    二漏至: ['中排', '无'],
    三漏至: ['SSR', '无'],
    四漏至: ['SSR', '无']
  },
  {
    name: '高压前轴封',
    一漏至: ['高排'],
    二漏至: ['中排', '无'],
    三漏至: ['SSR', '轴封加热器', '无'],
    四漏至: ['SSR', '轴封加热器', '无']
  },
  {
    name: '高压后轴封',
    一漏至: ['中排'],
    二漏至: ['SSR', '轴封加热器', '无'],
    三漏至: ['SSR', '轴封加热器', '无']
  },
  {
    name: '中压前轴封',
    一漏至: ['高排'],
    二漏至: ['中排', '无'],
    三漏至: ['SSR', '轴封加热器', '无'],
    四漏至: ['SSR', '轴封加热器', '无']
  },
  {
    name: '中压后轴封',
    一漏至: ['SSR', '轴封加热器'],
    二漏至: ['SSR', '轴封加热器', '无'],
    三漏至: ['SSR', '轴封加热器', '无']
  },
  {
    name: '低压轴封供汽',
    源自: ['SSR']
  },
  {
    name: '低压缸轴封漏汽',
    一漏流向: ['轴封加热器']
  },
  {
    name: 'SSR溢流',
    流向: ['凝汽器']
  }
] as const

// 在文件顶部添加
export type CorrectParaName = typeof allCorrectParas[number]['name'];
export type LeakageParaName = typeof allLeakageParas[number]['name'];

type CorrectInstance = {
  name: CorrectParaName
  [key: string]: any
}

type LeakageInstance = {
  name: LeakageParaName
  [key: string]: any
}

export const useCCPPStore = defineStore('ccpp', () => {

  // 机组型式选项
  const unitTypes = ref(['二拖一', '一拖一单轴', '一拖一分轴'])
  // 天然气流量类型选项
  const gasFlowTypes = ref(['涡轮流量计_体积流量', '涡轮流量计_脉冲计数', '质量流量计'])
  // 天然气流量类型选择
  const selectedGasFlowType = ref('涡轮流量计_体积流量')
  // 机组型式选择
  const selectedUnitType = ref('二拖一')
  const pulseToVolumeRatio = ref('1122.19')
  // 添加设计参数的响应式变量
  const designLHV = ref('')
  const designFlowRate = ref('')
  const designFlowRateUnit = ref('kg/h')
  const flowRateUnits = ref(['kg/h', 'Nm3/h', 'm3/h'])
  // 计算类型选项
  const calTypes = ref(['汽机出力', '汽机热耗', '燃机', '联合循环'])
  // 默认选择联合循环
  const calIndex = ref(['联合循环'])
  // 机组配置选项
  const hasTCA = ref(true)
  const hasFGH = ref(true)
  const threePres = ref(true)

  const sp = ref<object | null>(null)

  // 默认的修正参数
  const correct_paras = ref<(string | CorrectInstance)[]>(['大气压力', '大气温度', '环境相对湿度', '燃料气低位热值'])
  const leakage_paras = ref<LeakageInstance[]>([])
  const leakageRegion = ref<{
    yVar: string,
    x: { xVar: string, row: number, startCol: number, endCol: number, region: string },
    y: { yVar: string, row: number, startCol: number, endCol: number, region: string },
    curveRegion: string
  }[]>([])
  /**
   * 根据传入的点名从leakageRegion中获得对应的item
   * @param starts
   * @param ends
   */
  const getLeakageRegionItem = (starts: string, ends = null) => {
    if (ends === null) {
      // 如果只提供了starts参数，则查找yVar等于starts的项
      // console.log('starts', starts, 'ends', ends)
      return leakageRegion.value.find(item => item.yVar.startsWith(starts)) || null
    } else {
      // 如果同时提供了starts和ends参数，则查找yVar等于starts且curveRegion包含ends的项
      return leakageRegion.value.find(item =>
        item.yVar.startsWith(starts) &&
        item.yVar.endsWith(ends)
      ) || null
    }
  }
  const hasLeakageItem = (starts: string, ends = null) => {
    return getLeakageRegionItem(starts, ends) !== null
  }

  const logs = ref([])

  // 高级配置
  const hpConfig = ref({
    includesTCA: false,
    includesDesuperheater: false
  })

  const mpConfig = ref({
    includesFGH: false,
    includesDesuperheater: false
  })

  const lpConfig = ref({
    includesRecirculation: false
  })

  const condensateConfig = ref({
    includesFGH: false
  })

  const origin_sp = computed(() => {
    let res: object
    if (selectedUnitType.value === '二拖一') {
      res = eu.delete_item(eu.StandardPointCCPP, 'generator')
    } else if (selectedUnitType.value === '一拖一单轴') {
      res = eu.delete_item(eu.StandardPointCCPP, 'generator_turbine')
      res = eu.delete_item(res, 'generator_st')
      res = eu.delete_item(res, 'excitation_turbine')
      res = eu.delete_item(res, 'excitation_st')
    } else if (selectedUnitType.value === '一拖一分轴') {
      res = eu.delete_item(eu.StandardPointCCPP, 'generator')
    }
    return res
  })

  const dataScanned = ref(false)
  const scanData = () => {
    sp.value = systemdemo.refreshTestPoints(true, '数据整理', origin_sp.value)
    // console.log(sp.value)
    sp.value = systemdemo.scanDesignPoints('数据整理', sp.value) // 扫描设计工况的参数位置
    // console.log(sp.value)
    dataScanned.value = true
  }

  // 添加日志信息
  const printToTerm = (message: string) => {
    const timestamp = new Date().toLocaleTimeString()
    logs.value.push(`[${timestamp}] ${message}`)
  }


  /**
   * 根据修正参数名获取修正参数的设置项
   * @param name
   */
  const get_corr_para = (name: CorrectParaName): null | CorrectInstance => {
    // 检查 correct_paras 是否存在且有值
    // console.log('correct_paras:', correct_paras.value)
    if (!correct_paras.value) {
      return null
    }

    let result = null
    for (const item of correct_paras.value) {
      if (typeof item === 'string') {
        if (item === name) {
          // console.log('字符串项:', item)
          return null
        }
      } else {
        if ((item as any).name === name) {
          // console.log('配置型:', item)
          return item as CorrectInstance
        }
      }
    }
    return result
  }

  return {
    hpConfig,
    mpConfig,
    lpConfig,
    condensateConfig,
    origin_sp,
    sp,
    correct_paras,
    leakage_paras,
    unitTypes,
    gasFlowTypes,
    selectedGasFlowType,
    selectedUnitType,
    pulseToVolumeRatio,
    designLHV,
    designFlowRate,
    designFlowRateUnit,
    flowRateUnits,
    get_corr_para,
    printToTerm,
    allCorrectParas,
    scanData,
    logs,
    calTypes,
    calIndex,
    hasTCA,
    hasFGH,
    threePres,
    dataScanned,
    leakageRegion,
    getLeakageRegionItem
  }
})